//
//  XMMyMeetingView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/23.
//

#import "XMMyMeetingView.h"
#import "XMMyMeetingTopTabView.h"
#import "XMMyMeetingCell.h"
#import "XMMyMeetingDraftCell.h"

@interface XMMyMeetingView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSMutableArray *meetingModelArr;

@property (nonatomic, assign) NSInteger orderType;
@property (nonatomic, assign) BOOL isSelf;  //是否用户本身橱窗

@end

@implementation XMMyMeetingView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    self.navTitleLabel.text = @"我的见面";
    
    _orderType = 0;
    XMMyMeetingTopTabView *topTabView = [[XMMyMeetingTopTabView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(55.0))];
    [self addSubview:topTabView];
    [self addSubview:self.tbl];
    
    XMWeakSelf;
    topTabView.itemSelectBlock = ^(NSInteger selectIndex) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(stateSelect:)]) {
            [weakSelf.delegate stateSelect:@[@"10", @"1", @"-1"][selectIndex]];
        }
    };
}

- (void)setContentWithMeetingModelArr:(NSArray *)meetingModelArr
                                 page:(NSInteger)page
                            orderType:(NSInteger)orderType
                               userId:(nonnull NSString *)userId {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    _isSelf = [userModel.userId isEqualToString:userId];
    _orderType = orderType;
    [_tbl.mj_header endRefreshing];
    if (page == 1) {
        [self.meetingModelArr removeAllObjects];
    }
    if (meetingModelArr.count < 10) {
        [_tbl.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_tbl.mj_footer endRefreshing];
    }
    [self.meetingModelArr addObjectsFromArray:meetingModelArr];
    [_tbl reloadData];
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.meetingModelArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(8);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, KScaleWidth(8.0))];
    headView.backgroundColor = kColorHEX(0xF0F0F0);
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_orderType == 1) {
        return KScaleWidth(200.0);
    }
    else {
        return KScaleWidth(170.0);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_orderType == 1) {
        XMMyMeetingDraftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMyMeetingDraftCellName" forIndexPath:indexPath];
        XMMeetingModel *meetingModel = self.meetingModelArr[indexPath.section];
        [cell setContentWithOrderModel:meetingModel isSelf:_isSelf];
        XMWeakSelf;
        cell.meetingEditBlock = ^{ //编辑
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingEditWithServiceId:)]) {
                [weakSelf.delegate meetingEditWithServiceId:meetingModel.objectId];
            }
        };
        cell.meetingDeleteBlock = ^{ //删除
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingDeleteWithServiceId:)]) {
                [weakSelf.delegate meetingDeleteWithServiceId:meetingModel.objectId];
            }
        };
        return cell;
    }
    else {
        XMMyMeetingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMyMeetingCellName" forIndexPath:indexPath];
        XMMeetingModel *meetingModel = self.meetingModelArr[indexPath.section];
        [cell setContentWithOrderModel:meetingModel
                                  page:1
                             orderType:_orderType
                                isSelf:_isSelf];
        XMWeakSelf;
        cell.editBlock = ^{ //编辑
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingEditWithServiceId:)]) {
                [weakSelf.delegate meetingEditWithServiceId:meetingModel.objectId];
            }
        };
        cell.rePutawayBlock = ^{  //重新上架
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingReputawayWithServiceId:)]) {
                [weakSelf.delegate meetingReputawayWithServiceId:meetingModel.objectId];
            }
        };
        cell.soldOutBlock = ^{ //下架
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingSoldoutWithServiceId:)]) {
                [weakSelf.delegate meetingSoldoutWithServiceId:meetingModel.objectId];
            }
        };
        cell.deleteBlock = ^{ //删除
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(meetingDeleteWithServiceId:)]) {
                [weakSelf.delegate meetingDeleteWithServiceId:meetingModel.objectId];
            }
        };
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH + KScaleWidth(50.0), self.width, self.height - kNavagationBarH - KScaleWidth(50.0)) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        [_tbl registerClass:[XMMyMeetingCell class] forCellReuseIdentifier:@"XMMyMeetingCellName"];
        [_tbl registerClass:[XMMyMeetingDraftCell class] forCellReuseIdentifier:@"XMMyMeetingDraftCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
        XMWeakSelf;
        _tbl.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        _tbl.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMore)]) {
                [weakSelf.delegate loadMore];
            }
        }];
    }
    return _tbl;
}

- (NSMutableArray *)meetingModelArr {
    if (!_meetingModelArr) {
        _meetingModelArr = [NSMutableArray array];
    }
    return _meetingModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
